package com.carrental.vehicle.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.common.result.Result;
import com.carrental.vehicle.entity.Store;
import com.carrental.vehicle.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 门店控制器
 */
@Api(tags = "门店管理")
@RestController
@RequestMapping("/stores")
@RequiredArgsConstructor
public class StoreController {

    private final StoreService storeService;

    @ApiOperation("根据ID获取门店")
    @GetMapping("/{id}")
    public Result<Store> getStoreById(
            @ApiParam("门店ID") @PathVariable Long id) {
        return Result.success(storeService.getStoreById(id));
    }

    @ApiOperation("保存或更新门店")
    @PostMapping("/save")
    public Result<Boolean> saveOrUpdateStore(
            @ApiParam("门店信息") @Validated @RequestBody Store store) {
        return Result.success(storeService.saveOrUpdateStore(store));
    }

    @ApiOperation("删除门店")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteStoreById(
            @ApiParam("门店ID") @PathVariable Long id) {
        return Result.success(storeService.deleteStoreById(id));
    }

    @ApiOperation("分页查询门店")
    @GetMapping("/page")
    public Result<Page<Store>> pageStore(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("门店名称") @RequestParam(required = false) String name,
            @ApiParam("省份") @RequestParam(required = false) String province,
            @ApiParam("城市") @RequestParam(required = false) String city,
            @ApiParam("区县") @RequestParam(required = false) String district) {
        Page<Store> page = new Page<>(current, size);
        Store store = new Store();
        store.setName(name);
        store.setProvince(province);
        store.setCity(city);
        store.setDistrict(district);
        return Result.success(storeService.pageStore(page, store));
    }

    @ApiOperation("根据城市查询门店列表")
    @GetMapping("/list/city/{city}")
    public Result<List<Store>> listStoreByCity(
            @ApiParam("城市") @PathVariable String city) {
        return Result.success(storeService.listStoreByCity(city));
    }

    @ApiOperation("获取所有门店列表")
    @GetMapping("/list")
    public Result<List<Store>> listAllStores() {
        return Result.success(storeService.listAllStores());
    }
} 